package com.bytedance.scenedemo.architecture_patterns.mvvm;

import com.bytedance.scene.group.GroupScene;
import com.bytedance.scenedemo.ResourceTable;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.text.Font;

public class ViewModelSceneSamples extends GroupScene {
    public static final String KEY = "clickItem";
    public static int num;
    private Text txtViewModeSceneSample;

    @Override
    public ComponentContainer onCreateView(LayoutScatter inflater,
                                           ComponentContainer container, Intent savedInstanceState) {
        ComponentContainer componentContainer = (ComponentContainer) inflater.parse(ResourceTable.Layout_view_model_scene_sample, container, false);
        initView(componentContainer);
        return componentContainer;
    }

    @Override
    public void onSaveInstanceState(Intent intent) {
        super.onSaveInstanceState(intent);
        intent.setParam(KEY, num);
    }

    @Override
    public void onActivityCreated(Intent intent) {
        super.onActivityCreated(intent);
        if (ViewModelSceneAbility.getPacMap() != null) {
            num = ViewModelSceneAbility.getPacMap().getIntValue(KEY);
        }
        setNum();
    }

    private void initView(ComponentContainer componentContainer) {
        Button btnViewModeSceneSample = (Button) componentContainer.findComponentById(ResourceTable.Id_btn_view_mode_scene_sample);
        Font font = new Font.Builder(btnViewModeSceneSample.getText()).setWeight(Font.BOLD).build();
        btnViewModeSceneSample.setFont(font);
        txtViewModeSceneSample = (Text) componentContainer.findComponentById(ResourceTable.Id_txt_view_mode_scene_sample);
        btnViewModeSceneSample.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                num++;
                setNum();
            }
        });
    }

    private void setNum() {
        if (txtViewModeSceneSample.getVisibility() == Component.INVISIBLE && num != 0) {
            txtViewModeSceneSample.setVisibility(Component.VISIBLE);
        } else if (num == 0) {
            txtViewModeSceneSample.setVisibility(Component.INVISIBLE);
        }
        txtViewModeSceneSample.setText("" + num);
    }
}
